package com.http;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.internal.tls.OkHostnameVerifier;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wangwei on 2017/4/7.
 */

public class RetrofitFactory {

    private  static  volatile  Retrofit instance ;
    private  static volatile  Retrofit weatherInstance;
    private  static volatile  Retrofit pm25Instance;
    private static final OkHttpClient NORMAL_CLIENT;


    static {
        final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.v("Weather","----"+message);

            }
        });
        httpLoggingInterceptor.setLevel(httpLoggingInterceptor.getLevel().BODY);
        final TrustManagerDelegate trustManager = new TrustManagerDelegate().trustWhatSystemTrust();
        NORMAL_CLIENT = new OkHttpClient.Builder()
                .connectionPool(new ConnectionPool(5, 60, TimeUnit.SECONDS))
                .connectTimeout(30000, TimeUnit.MILLISECONDS)
                .readTimeout(30000, TimeUnit.MILLISECONDS)
                .addInterceptor(httpLoggingInterceptor)
                .sslSocketFactory(trustManager.createSSLSocketFactory(), trustManager)
                .hostnameVerifier(OkHostnameVerifier.INSTANCE)
                .build();



    }


    private  RetrofitFactory(){}
    public static Retrofit getIntance(){
         if (instance == null){
             synchronized (RetrofitFactory.class){
                 if (instance == null){
                     instance = new Retrofit.Builder()
                             .baseUrl("http://restapi.amap.com/")
                             .addConverterFactory(GsonConverterFactory.create())
                             .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                             .build();
                 }
             }
         }
         return instance;

    }

    public static Retrofit getWeatherInstance(){
        if (weatherInstance == null){
            synchronized (RetrofitFactory.class){
                if (weatherInstance == null){
                    weatherInstance = new Retrofit.Builder()
                            .baseUrl("https://free-api.heweather.com/")
                            .client(NORMAL_CLIENT)
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                            .build();
                }
            }
        }
        return weatherInstance;

    }
    public static Retrofit getPm25Instance(){
        if (pm25Instance == null){
            synchronized (RetrofitFactory.class){
                if (pm25Instance == null){
                    pm25Instance = new Retrofit.Builder()
                            .baseUrl("http://www.help.bj.cn/")
                            .client(NORMAL_CLIENT)
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                            .build();
                }
            }
        }
        return pm25Instance;

    }


}
